package com.auto.code.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auto.code.core.service.ReadTableService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auto.code.core.domain.GenTableEntity;
import com.auto.code.core.domain.TemplateFile;
import com.auto.code.core.enums.DataDictionaryEnum;
import com.auto.code.domain.TemplateDTO;
import com.auto.code.service.GenerationService;
import com.auto.code.util.GenCodeUtil;

/**
 * 
 * @desc 
 * @author meikb
 * @date 2019年10月9日	
 *
 */
@Service
public class GenerationServiceImpl implements GenerationService{

	@Autowired
	private ReadTableService readTableService;

	@Override
	public List<TemplateFile> getTemplateFileList(String dbName, String dbUrl, List<TemplateDTO> templateList, List<GenTableEntity> dbTables, List<String> tableList, String packagePath, String projectName, String basePath) throws Exception{
		List<TemplateFile> templateFileList = new ArrayList<>();
		for(TemplateDTO dto : templateList) {
			if(dto.getGenWay() == 2){  //单个文件
				TemplateFile file = null;
				if(dto.getFileName().contains("pom")){  //生成pom
					file = getPomFile(dto, packagePath, projectName);
				}else{
					//处理application类
					file = getSingleFile(dto, packagePath, projectName, dbUrl, basePath);
					if(dto.getFileName().contains("ModuleURL")){
						file = genModulURL(file, dbTables);
					}
				}
				templateFileList.add(file);
			}else{	//表文件
				for(GenTableEntity table : dbTables) {
					if(tableList.contains(table.getTableName())) {

						GenTableEntity tableEntity = readTableService.readField(dbName, table.getTableName());

						TemplateFile file = new TemplateFile();
						String content = dto.getContent();

						//解析模板
						String newTemplate = replaceData(content, tableEntity, packagePath, projectName, null, basePath);
						file.setContent(newTemplate);

						//获取文件名称
						String fileName = getFileName(dto, tableEntity, projectName);
						file.setName(fileName);

						//获取路径
						String filePath = getFilePath(dto, packagePath).replaceAll("\\$\\{table_obj_name\\}", GenCodeUtil.getObjName(table.getTableName()));

						file.setPath(filePath);

						templateFileList.add(file);
					}
				}
			}
		}
		return templateFileList;
	}

	private TemplateFile getPomFile(TemplateDTO dto, String packagePath, String projectName) {
		TemplateFile file = new TemplateFile();
		String content = dto.getContent();
		String newTemplate = replaceData(content, null, packagePath, projectName, null, null);
		file.setContent(newTemplate);
		file.setName(dto.getFileName());
		String filePath = "";
		file.setPath(filePath);
		return file;
	}

	private TemplateFile genModulURL(TemplateFile file, List<GenTableEntity> dbTables) {
		file.setContent(file.getContent().replaceAll("\\$\\{project_urls\\}", GenCodeUtil.getModulURL(dbTables)));
		return file;
	}

	private TemplateFile getSingleFile(TemplateDTO dto, String packagePath, String projectName, String dbUrl, String basePath) {
		TemplateFile file = new TemplateFile();
		String content = dto.getContent();
		String newTemplate = replaceData(content, null, packagePath, projectName, dbUrl, basePath);

		file.setContent(newTemplate);
		//获取文件名称
		String fileName = getFileName(dto, null, projectName);
		file.setName(fileName);
		//获取路径
		String filePath = getFilePath(dto, packagePath);

		file.setPath(filePath);

		return file;
	}

	private String getFilePath(TemplateDTO dto, String packagePath) {
		if(dto.getFilePath().equals("java")){
			if(dto.getPath().equals("/")){
				return "src/main/" + dto.getFilePath() + "/" + packagePath.replaceAll("\\.", "\\/");
			}else{
				return "src/main/" + dto.getFilePath() + "/" + packagePath.replaceAll("\\.", "\\/") + "/" + dto.getPath();
			}
		}else{
			if(dto.getPath().equals("/")){
				return "src/main/" + dto.getFilePath();
			}
			return "src/main/" + dto.getFilePath() + "/" + dto.getPath();
		}
	}

	private String getFileName(TemplateDTO dto, GenTableEntity table, String projectName) {
		String fileName = replaceData(dto.getFileName(), table, null, projectName, null, null);
		return fileName;
	}


	private String replaceData(String temp, GenTableEntity table, String packagePath, String projectName, String dbUrl, String basePath) {
		if(null != table){
			temp = temp.replaceAll("\\$\\{get_set_field\\}", GenCodeUtil.getGetSetFiled(table.getFields()))
					.replaceAll("\\$\\{get_set_method\\}", GenCodeUtil.getGetSetMethod(table.getFields()))
					.replaceAll("\\$\\{table_class_name\\}", GenCodeUtil.lineToHump(table.getTableName()))
					.replaceAll("\\$\\{table_name\\}", table.getTableName())
					.replaceAll("\\$\\{table_name_upper\\}", table.getTableName().toUpperCase())
					.replaceAll("\\$\\{table_comment\\}", StringUtils.isEmpty(table.getChinaName())?"" : table.getChinaName())
					.replaceAll("\\$\\{table_obj_name\\}", GenCodeUtil.getObjName(table.getTableName()))
					.replaceAll("\\$\\{sql_map_column\\}", GenCodeUtil.getSqlMapClumn(table.getFields()))
					.replaceAll("\\$\\{sql_column\\}", GenCodeUtil.getSqlColumn(table.getFields()))
					.replaceAll("\\$\\{sql_update_filed\\}", GenCodeUtil.getUpdateFiled(table.getFields()))
					.replaceAll("\\$\\{sql_column_attr\\}", GenCodeUtil.getSqlColumnAttr(table.getFields()))
					.replaceAll("\\$\\{sql_column_attr2\\}", GenCodeUtil.getSqlColumnAttr2(table.getFields()))
					.replaceAll("\\$\\{sql_condition\\}", GenCodeUtil.getSqlCondition(table.getFields()))
					.replaceAll("\\$\\{js_get_params\\}", GenCodeUtil.getJsTableFiledVal(table.getFields()))
					.replaceAll("\\$\\{js_send_params\\}", GenCodeUtil.getSendJsTableFiledVal(table.getFields()))
					.replaceAll("\\$\\{th_filed\\}", GenCodeUtil.getThFiled(table.getFields()))
					.replaceAll("\\$\\{td_filed\\}", GenCodeUtil.getTdFiled(table.getFields()))
			;
		}
		if(StringUtils.isNotEmpty(projectName)){
			temp = temp.replaceAll("\\$\\{project_name\\}", projectName);
			temp = temp.replaceAll("\\$\\{project_name_hump\\}", GenCodeUtil.lineToHump(projectName));

			String serviceName = "";
			String serviceProjectName = projectName;
			if(projectName.endsWith("-mgt") || projectName.endsWith("-api")){
				serviceProjectName = projectName.replaceAll("-mgt", "").replaceAll("-api","");
				serviceName = serviceProjectName.replaceAll("-", ".");
			}
			temp = temp.replaceAll("\\$\\{service_project_name\\}", serviceProjectName);
			temp = temp.replaceAll("\\$\\{service_name\\}", serviceName);
		}

		//包路径
		if(StringUtils.isNotEmpty(packagePath)){
			temp = temp.replaceAll("\\$\\{package\\}", packagePath);
		}

		//访问路径
		if(StringUtils.isNotEmpty(basePath)){
			temp = temp.replaceAll("\\$\\{base_path\\}", basePath);
		}

		if(StringUtils.isNotEmpty(dbUrl)){
			temp = temp.replaceAll("\\$\\{db_url\\}", dbUrl);
		}

		return temp.replaceAll("&gt;", ">").replaceAll("&lt;", "<")
			   .replaceAll("&amp;", "&");
	}
}